//-----------------------------------------------------------------------------
// Copyright (c) 2008 Intel Corporation
//
// DISTRIBUTABLE AS SAMPLE SOURCE SOFTWARE
//
// This Distributable As Sample Source Software is subject to the terms and
// conditions of the Intel Software License Agreement provided with the Intel(R)
// Media Processor Software Development Kit.
//------------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include <GLES/gl.h>


#include <czDisplay.h>
#include <WindowCanmore.h>
#include <ContextEGL1.h>

// Plane size and position
#define ASPECT ((GLfloat)1280 / (GLfloat)720)
#define INTEGER_MAX  2147483647UL

static GLfloat world_width = 100.0f * ASPECT;
static GLfloat world_height = 100.0f;

static GLfloat square_x = 0.0f, square_y = 0.0f;
static GLfloat square_size = 25;

static GLfloat speed_x = 1.0f;
static GLfloat speed_y = 1.0f;


static bool TestEGLError(char* pszLocation)
{
  /*
  eglGetError returns the last error that has happened using egl,
  not the status of the last called function. The user has to
  check after every single egl call or at least once every frame.
  */
  EGLint iErr = eglGetError();
  if (iErr != EGL_SUCCESS)
  {
    printf("%s failed (%d).\n", pszLocation, iErr);
    return false;
  }

  return true;
}


// Initializes OpenGL by setting up matricies and enabling necessary state.
static void setup_gl(void)
{
    glViewport(0, 0, 1280, 720);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrthof(-100.0 * ASPECT, 100.0 * ASPECT, -100.0, 100.0, 1.0, -1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0.0f, 0.0f, 1.0f, 0.8f);
    glEnableClientState(GL_VERTEX_ARRAY);
}


// Updates the position and direction of the rectangle
static void update(void)
{
    if (square_x > (world_width - square_size) ||
        square_x < (-world_width + square_size))
    {
        speed_x = -speed_x;
    }

    if (square_y > (world_height - square_size) ||
        square_y < (-world_height + square_size))
    {
        speed_y = -speed_y;
    }

    square_x += speed_x;
    square_y += speed_y;
}


// Renders the bouncing rectangle
static void render(void)
{
    GLfloat verts[] =
    {
        square_x - square_size, square_y - square_size,
        square_x + square_size, square_y - square_size,
        square_x - square_size, square_y + square_size,
        square_x + square_size, square_y + square_size
    };
    static GLubyte indices[] = { 0, 1, 2, 3 };

    glClear(GL_COLOR_BUFFER_BIT);

    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    glVertexPointer(2, GL_FLOAT, 0, verts);
    glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
}


int main(int argc, char* argv[])
{
    int frame_count = 0;

    if (argc == 2)
    {
        frame_count = atoi(argv[1]);
        if (frame_count <= 0 || frame_count > INTEGER_MAX )
        {
            frame_count = 1;
        }
    }
    else
    {
        fprintf(stdout, "\nUsage:  bounce [ frames ]\n");
        fprintf(stdout, "where:\n\n");
        fprintf(stdout, "  frames:  a positive number for the number of frames to run.\n");
        fprintf(stdout, "           If it is less than 30, it will be set to 30.\n");

        exit(1);
    }

    ContextEGL1 ctx;
    WindowClass wnd(&ctx, "title", 1280, 720);

    setup_gl();

    while (wnd.isRun())
    {
        update();
        render();

        wnd.update();

        if(frame_count<=0)
          break;
        frame_count--;
    }

    return 0;
}
